<?php
if(!defined('BLOG'))exit('不作死就不会死');
define('CHARSET','utf-8');
error_reporting(E_ALL);//控制错误级别
if(!ob_start("ob_gzhandler")) ob_start();  //开启gzip
$protocol = $_SERVER['SERVER_PORT'] !=80?'https://':'http://';
define('WEB_PATH',$protocol.$_SERVER['HTTP_HOST'].'/');
define('IMG_PATH',WEB_PATH.'statics'.DIRECTORY_SEPARATOR.'images/');
define('JS_PATH',WEB_PATH.'statics'.DIRECTORY_SEPARATOR.'js/');
define('CSS_PATH',WEB_PATH.'statics'.DIRECTORY_SEPARATOR.'css/');
ini_set('date.timezone','Asia/Shanghai'); // 'Asia/Shanghai' 为上海时区
define('REFERER',isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:WEB_PATH);
/**
 * Class base
 */
class base{
    /**
     * @param string $file  文件名/完整文件名
     * @param string $key   配置的key名
     * @return array|bool
     */
    public static function create_app(){
        base::load_class('route','',1);
        base::auto_load_func();
        base::auto_load_func(R_M);
        base::Run(R_M,R_C,R_A);
    }
    public static function load_config($file='system',$key=''){
        if((string)$file){
            $path = ROOTPATH.'core'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.$file.'_config.php';
            $config = @include $path;
            if(is_array($config)){
                $config =  $key  != ''?$config[$key]:$config;
                return $config;
            }else{
                return  false;
            }
        }
        if(is_file($file)){
            $config = include $file;
            if(is_array($config)){
                return  isset($key)?$config[$key]:$config;
            }else{
                return  false;
            }
        }
        return false;
    }

    /**
     * @param $classname    类名
     * @param string $module
     * @param bool $init
     * @return bool
     */
    public static function load_class($classname,$module='',$init=false){
        $module = ($module != '')?'module'.DIRECTORY_SEPARATOR.$module:'libs';
        $path = ROOTPATH.'core'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR.$classname.'.class.php';
        $key = md5($path);
        static $array = array();
        if(isset($array[$key])){
            return $array[$key];
        }
        @include $path;
        if(is_file($path)){
            if($init){
                $array[$key] = new $classname();
                return  $array[$key];
            }else{
                $array[$key] = true;
                return $array[$key];
            }
        }
        return false;
    }

    /**
     * @param $func_name    函数文件名
     * @param string $module    模块名称
     */
    public static function load_func($func_name,$module=''){
        $module = $module !=''?$module:'libs';
        $path = ROOTPATH.'core'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'functions'.DIRECTORY_SEPARATOR.$func_name.'func.php';
        @include $path;
    }

    /**
     * 自动加载函数
     */
    public static function auto_load_func($module=''){
        $module = $module != ''?'module'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'functions':'libs'.DIRECTORY_SEPARATOR.'functions';
        $path = ROOTPATH.'core'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'global.func.php';
        if(is_file($path)){
            @include $path;
        }
       return false;
    }

    public static function load_model($model_name){
        static $array = array();
        $path = ROOTPATH.'core'.DIRECTORY_SEPARATOR.'model'.DIRECTORY_SEPARATOR.$model_name.'.php';
        $key = md5($path);
        if(isset($array[$key])){
            return $array[$key];
        }
        if(is_file($path)){
            include $path;
            $array[$key] = new $model_name();
            return $array[$key];

        }
        return false;

    }
    /**
     * @param $module
     * @param $classname
     * @param $action
     * @return bool
     */
    public static function Run($module,$classname,$action){
        $module = 'module'.DIRECTORY_SEPARATOR.$module;
        $path = ROOTPATH.'core'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$classname.'.php';
        if(is_file($path)){
            @include $path;
            $instance = new $classname();
            $instance->$action();
        }
        return false;
    }
}
?>